# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import re

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

# 根据京东URL商品详细页面，提取评价信息
class QiChaCha():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Cookie": "QCCSESSID=4vkmvrptlsoi4nl090r67e88f6; UM_distinctid=16edb951f03972-05ec41f30aa4ce-2393f61-144000-16edb951f0464f; CNZZDATA1254842228=720282007-1575640338-https%253A%252F%252Fsp0.baidu.com%252F%7C1575640338; Hm_lvt_3456bee468c83cc63fb5147f119f1075=1575642014; zg_did=%7B%22did%22%3A%20%2216edb95225e1f3-01ef46de16b3d1-2393f61-144000-16edb95225fb31%22%7D; hasShow=1; _uab_collina=157564201436531560202225; acw_tc=8bd7c09f15756420147393838e4397de8211474588307361f7649089b3; zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201575642014307%2C%22updated%22%3A%201575642020104%2C%22info%22%3A%201575642014310%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22sp0.baidu.com%22%7D; Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1575642020",
            "DNT": "1",
            "Host": "www.qichacha.com",
            "Pragma": "no-cache",
            "Referer": "https://www.qichacha.com/",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": ua.random
        }


    def get_contents(self):
        data = {
            "key": "辽宁圣维机电"
        }
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "DNT": "1",
            "Host": "api.meituan.com",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
        }
        url = "https://www.qichacha.com/search"
        html = HttpUtils.do_request("GET", url, self.headers, data)
        selector = etree.HTML(html.text)
        root = selector.xpath('//tr[@class=" "][1]')
        tmp = selector.xpath('//tr[@class=" "][1]/td[3]/a/@onclick')[0]
        company = re.findall(r"内容名称':(.*?),", tmp)[0].replace(" ", "").replace("'", "")# 公司名称
        url = re.findall(r"内容链接':(.*?),", tmp)[0].replace(" ", "").replace("'", "")  # 链接
        fr = selector.xpath('//tr[@class=" "][1]/td[3]/p[1]/a/text()')[0]  # 法人代表
        zb = selector.xpath('//tr[@class=" "][1]/td[3]/p[1]/span/text()')[0]  # 注册资本
        clrq = selector.xpath('//tr[@class=" "][1]/td[3]/p[1]/span/text()')[1]  # 成立日期
        tel = selector.xpath('//tr[@class=" "][1]/td[3]/p[2]/span/text()')[0].replace("\n", "").replace(" ", "").strip()
        address = selector.xpath('//tr[@class=" "][1]/td[3]/p[3]/text()')[0].replace("\n", "").replace(" ", "").strip()


if __name__ == '__main__':
    qichacha = QiChaCha()
    qichacha.get_contents()